package com.example.myapplication.dao;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;

import com.example.myapplication.entity.Book;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;


public class FindorSort {

    public ArrayList<Book> arrayList = new ArrayList<>();
    public ArrayList<Bitmap> picarrayList = new ArrayList<>();
    public static ArrayList<Book> collectedBook = new ArrayList<>();
    public static ArrayList<Bitmap> collectedPic = new ArrayList<>();
public int length;
    public boolean ischanged = false;

    public void getBooksByFindorSort(String key, String content, Handler handler) {
        new Thread(() -> {
            HttpURLConnection conn;
            try {
                URL url = new URL(HttpDao.urladd + "FindorSort");
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");

                //上传参数 实现post
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(conn.getOutputStream());
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                bufferedWriter.write("key=" + key + "&content=" + content);
                bufferedWriter.flush();
                //获得数据传入flag 并改变ischanged

                DataInputStream inputStream = new DataInputStream(conn.getInputStream());
                ObjectInputStream objectInputStream = new ObjectInputStream(conn.getInputStream());
                int size = inputStream.readInt();
                for (int i = 0; i < size; i++) {

                    Book book = (Book) objectInputStream.readObject();
                    arrayList.add(book);


                    int available = inputStream.readInt();

                    byte[] data;

                    InputStream in = conn.getInputStream();
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    byte[] bytes = new byte[3000];
                    int length = -1;
                    while ((length = in.read(bytes)) != -1 && available > length) {
                        out.write(bytes, 0, length);
                        available = available - length;
                    }
                    data = out.toByteArray();


                    picarrayList.add(BitmapFactory.decodeByteArray(data, 0, data.length));
                }
                inputStream.close();
                objectInputStream.close();
                Message message = handler.obtainMessage();
                message.what = 2;
                message.arg1=size;
                handler.sendMessage(message);

            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }).start();

    }


}
